//
//  ECUIAlertView_CourseDetail.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/3/7.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUIAlertView_CourseDetail.h"
#import "ZJAnimationPopView.h"
@interface ECUIAlertView_CourseDetail()
@property(nonatomic,strong)ZJAnimationPopView *popView;        //蒙版图层view
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)UIImageView *tipView;
@property(nonatomic,strong)UILabel * textLabel;//显示的提示内容
@property(nonatomic,strong)UIButton *confirmBtn;//确定
@property(nonatomic,strong)UIButton *cancelBtn;//取消

@end
@implementation ECUIAlertView_CourseDetail
- (instancetype)init
{
    if (self = [super init]) {
        [self.contentView addSubview:self.tipView];
        [self.contentView addSubview:self.textLabel];
    }
    return self;
    
}
- (instancetype)initWithTitle:(NSAttributedString *)title cancleTitle:(NSString *)cancelTitle confirmTitle:(NSString *)confirmTitle
{
    if (self = [self init]) {
        self.title = title;
        [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.contentView);
            make.top.equalTo(self.contentView);
            make.left.equalTo(self.contentView);
            make.height.equalTo(self.tipView.mas_width).multipliedBy(0.3197);
        }];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.contentView);
            make.width.equalTo(self.contentView).multipliedBy(0.75);
            make.height.equalTo(self.contentView).multipliedBy(0.30);
        }];
        self.textLabel.attributedText = self.title;
        UIView *bottomView = [[UIView alloc]init];
        [self.contentView addSubview:bottomView];
        self.contentView.userInteractionEnabled = YES;
        bottomView.userInteractionEnabled = YES;
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.contentView);
            make.top.equalTo(self.textLabel.mas_bottom);
            make.bottom.equalTo(self.contentView);
            make.left.equalTo(self.contentView);
        }];
        if (confirmTitle) {
            [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
            [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];

            [bottomView addSubview:self.confirmBtn];
            [bottomView addSubview:self.cancelBtn];
            [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(bottomView.mas_centerX);
                make.width.equalTo(@(kProportion_Width(540)));
                make.height.equalTo(@(kProportion_Height(114)));
                make.top.equalTo(bottomView).offset(5);
                                    
            }];
            
            [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.confirmBtn.mas_centerX);
                make.top.equalTo(self.confirmBtn.mas_bottom).offset(5);
                make.bottom.greaterThanOrEqualTo(bottomView.mas_bottom).offset(-5);
                make.width.equalTo(@(kProportion_Width(540)));
                make.height.equalTo(@(kProportion_Height(114)));
            }];
        } else {
            [bottomView addSubview:self.cancelBtn];
            [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
            [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(bottomView.mas_centerX);
                make.centerY.equalTo(bottomView);
                make.width.equalTo(@(kProportion_Width(540)));
                make.height.equalTo(@(kProportion_Height(114)));
            }];
        }
    }
    return self;
    
}
- (UIButton *)confirmBtn
{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmBtn.backgroundColor = [UIColor colorWithHexString:@"#FF6701"];
        _confirmBtn.layer.masksToBounds = YES;
        _confirmBtn.layer.cornerRadius = 3.f;
        [_confirmBtn addTarget:self action:@selector(action_confirm:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}
- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor colorWithHexString:@"#FF6700"] forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
        _cancelBtn.layer.masksToBounds = YES;
        _cancelBtn.layer.cornerRadius = 3.f;
        [_cancelBtn addTarget:self action:@selector(action_cancel:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _cancelBtn;
}
- (void)action_confirm:(UIButton *)sender
{
    [self.popView dismiss];
    if (self.block_confirm) {
        self.block_confirm();
    }
}

- (void)action_cancel:(UIButton *)sender
{
    [self.popView dismiss];

    if (self.block_cancel) {
        self.block_cancel();

    }
    
}
- (void)showAlert
{
 
    [self.popView pop];
}
- (ZJAnimationPopView *)popView
{
    if (!_popView) {
        _popView = [[ZJAnimationPopView alloc] initWithCustomView:self.contentView popStyle:ZJAnimationPopStyleShakeFromLeft dismissStyle:ZJAnimationDismissStyleDropToRight];
        //显示时点击背景是否移除弹框
        _popView.isClickBGDismiss = YES;
        WeakSelf(weakSelf);
        _popView.dismissComplete = ^{
            [weakSelf action_cancel:nil];
        };
    }
    return _popView;
}
- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kProportion_Width(750), kProportion_Height(940))];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.cornerRadius = 3.f;
    }
    return _contentView;
}
- (UIImageView *)tipView
{
    if (!_tipView) {
        _tipView = [[UIImageView alloc] init];
        [self.contentView addSubview:_tipView];
        _tipView.image = [UIImage imageNamed:@"tips_coursedetail"];//根据不同类别设置不同上部图片
    }
    return _tipView;
}
- (UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(self.tipView.frame), CGRectGetWidth(self.contentView.frame)-30, 100)];
        _textLabel.numberOfLines = 0;
        
    }
    return _textLabel;
}
@end
